package com.smartleanx.module.hrm.controller.admin.payrollapproverecord;

import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.util.collection.CollectionUtils;
import com.smartleanx.framework.common.util.object.BeanUtils;
import com.smartleanx.module.hrm.controller.admin.payrollapproverecord.vo.PayrollApproveRecordPageReqVO;
import com.smartleanx.module.hrm.controller.admin.payrollapproverecord.vo.PayrollApproveRecordPageRespVO;
import com.smartleanx.module.hrm.controller.admin.payrollapproverecord.vo.PayrollApproveRecordRespVO;
import com.smartleanx.module.hrm.dal.dataobject.payroll.PayrollDO;
import com.smartleanx.module.hrm.dal.dataobject.payrollapproverecord.PayrollApproveRecordDO;
import com.smartleanx.module.hrm.intergration.AdminUserServiceProvider;
import com.smartleanx.module.hrm.intergration.DeptServiceProvider;
import com.smartleanx.module.hrm.service.payroll.PayrollService;
import com.smartleanx.module.hrm.service.payrollapproverecord.PayrollApproveRecordService;
import com.smartleanx.module.system.api.dept.dto.DeptRespDTO;
import com.smartleanx.module.system.api.user.dto.AdminUserRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.smartleanx.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 薪资审批记录")
@RestController
@RequestMapping("/hrm/payroll-approve-record")
@Validated
public class PayrollApproveRecordController {

    @Resource
    private PayrollApproveRecordService payrollApproveRecordService;
    
    @Resource
    private PayrollService payrollService;

    @Resource
    private AdminUserServiceProvider adminUserServiceProvider;

    @Resource
    private DeptServiceProvider deptServiceProvider;

    @GetMapping("/get")
    @Operation(summary = "获得薪资审批记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasAnyPermissions('hrm:payroll-approve-record:query', 'hrm:payroll-approve-record-detail:query')")
    public CommonResult<PayrollApproveRecordRespVO> getPayrollApproveRecord(@RequestParam("id") Long id) {
        PayrollApproveRecordDO record = payrollApproveRecordService.getRecord(id);
        PayrollApproveRecordRespVO respVO = BeanUtils.toBean(record, PayrollApproveRecordRespVO.class);
        PayrollDO payroll = payrollService.getPayroll(record.getPayrollId());
        if(Objects.nonNull(payroll)){
            respVO.setYear(payroll.getYear());
            respVO.setMonth(payroll.getMonth());
            respVO.setPayrollState(payroll.getState());
        }
        return success(respVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得薪资审批记录分页(查看审核进度)")
    @PreAuthorize("@ss.hasPermission('hrm:payroll-approve-record:query')")
    public CommonResult<PageResult<PayrollApproveRecordPageRespVO>> getPayrollApproveRecordPage(@Valid PayrollApproveRecordPageReqVO pageReqVO) {
        PageResult<PayrollApproveRecordDO> pageResult = payrollApproveRecordService.getRecordPage(pageReqVO);
        List<PayrollApproveRecordDO> list = pageResult.getList();
        // 准备
        List<AdminUserRespDTO> userList = adminUserServiceProvider.getUserList(
                CollectionUtils.convertSet(list, record -> Long.valueOf(record.getCreator())));
        Map<String, AdminUserRespDTO> userMap = CollectionUtils.convertMap(userList, user -> user.getId().toString());
        Map<Long, String> deptMap = CollectionUtils.convertMap(deptServiceProvider.getDeptList(
                CollectionUtils.convertSet(userList, AdminUserRespDTO::getDeptId)), DeptRespDTO::getId, DeptRespDTO::getName);
        // 拼装
        List<PayrollApproveRecordPageRespVO> respVOList = CollectionUtils.convertList(list, record -> {
            PayrollApproveRecordPageRespVO respVO = BeanUtils.toBean(record, PayrollApproveRecordPageRespVO.class);
            if (userMap.containsKey(record.getCreator())) {
                AdminUserRespDTO user = userMap.get(record.getCreator());
                respVO.setCreatorName(user.getNickname());
                respVO.setDeptId(user.getDeptId());
                if (deptMap.containsKey(user.getDeptId())) {
                    respVO.setDeptName(deptMap.get(user.getDeptId()));
                }
            }
            return respVO;
        });
        return success(new PageResult<>(respVOList, pageResult.getTotal()));
    }
}